<?php
namespace addon\cms\controller;

use app\common\controller\AddonAdminBase;
use tpfcore\Core;
use tpfcore\helpers\Json;
use think\Request;
// +----------------------------------------------------------------------
// | Author: yaoyihong <510974211@qq.com>
// +----------------------------------------------------------------------
/**
 * ============================================================================
 * 版权所有 2017-2077 tpframe工作室，并保留所有权利。
 * @link http://www.tpframe.com/
 * @copyright Copyright (c) 2017 TPFrame Software LLC
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 单页
 * ============================================================================
 */
class Spage extends AddonAdminBase
{
    public function index()
    {

        $data = $this->param;
        $where=[];
        if(!empty($data['sign'])){
            $where['sign']=$data['sign'];
        }

        $start_time = empty($data['start_time'])?"":strtotime($data['start_time']);
        $end_time = empty($data['end_time'])?"":strtotime($data['end_time']);
        if($start_time && empty($end_time)){
            $where['datetime']=["exp",">=$start_time"];
        }
        else if(empty($start_time) && $end_time){
            $where['datetime']=["exp","<=$end_time"];
        }
        else if($start_time && $end_time){
            $where['datetime']=["between","$start_time,$end_time"];
        }else{

        }

        if(!empty($data['keyword'])){
            $where['title']=["like","%{$data['keyword']}%"];
        }

        $spageModel=Core::loadAddonModel($this->name);
        $sign_list= $spageModel->getSpage(["group"=>['group' => 'sign'],"field"=>"sign"]);
        $this->addonTemplate("index",[
            "list"=>$spageModel->getSpage([
                "field"=>"id,title,sign,template,datetime,make_to_html,filename,updatetime",
                "paginate"=>["rows"=>20],
                "order"=>"datetime desc",
                "where"=>$where
            ]),
            "sign_list"=>$sign_list
        ]);
    }
    public function edit(){
        if(IS_POST){
            $this->jump(Core::loadAddonModel($this->name)->saveSpage($this->param));
        }
        $spageModel=Core::loadAddonModel($this->name);
        $sign_list= $spageModel->getSpage(["group"=>['group' => 'sign'],"field"=>"sign"]);
        $list = $spageModel->getSpage(["where"=>['id'=>$this->param['id']]]);
        $this->addonTemplate("edit",[
            'list'=>$list,
            "sign_list"=>$sign_list
        ]);
    }
    public function add()
    {
        if(IS_POST){
            $this->jump(Core::loadAddonModel($this->name)->saveSpage($this->param));
        }
        $spageModel=Core::loadAddonModel($this->name);
        $sign_list= $spageModel->getSpage(["group"=>['group' => 'sign'],"field"=>"sign"]);
        $this->addonTemplate("add",[
            "sign_list"=>$sign_list
        ]);
    }
    public function del()
    {
        $this->jump(Core::loadAddonModel($this->name)->delSpage($this->param));
    }
} 
